package com.cvai.config;

import com.cvai.aiservice.ChatAssistant;
import com.cvai.aiservice.ImageChatAssistant;
import com.cvai.properties.LLMproperties;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.embedding.qdrant.QdrantEmbeddingStore;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LLMConfig {

    @Autowired
    private LLMproperties llMproperties;

    // 通用模型（默认Bean名称与方法名一致）
    @Bean
    public StreamingChatLanguageModel streamingChatLanguageModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(llMproperties.getAlyKey())
                .modelName(llMproperties.getCommonModel())
                .baseUrl(llMproperties.getBaseUrl())
                .build();
    }

    // 图片专用模型（显式指定Bean名称）
    @Bean(name = "imageStreamingModel")
    public StreamingChatLanguageModel streamingChatLanguageModel1() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(llMproperties.getAlyKey())
                .modelName(llMproperties.getPictureModel())
                .baseUrl(llMproperties.getBaseUrl())
                .build();
    }


    @Bean
    public ChatAssistant chatAssistant(
            @Qualifier("streamingChatLanguageModel") StreamingChatLanguageModel model) {
        return AiServices.builder(ChatAssistant.class)
                .streamingChatLanguageModel(model)
                .chatMemoryProvider(id -> MessageWindowChatMemory.withMaxMessages(5))
                .build();
    }

    @Bean
    public ImageChatAssistant imageChatAssistant(
            @Qualifier("imageStreamingModel") StreamingChatLanguageModel model) {
        return AiServices.builder(ImageChatAssistant.class)
                .streamingChatLanguageModel(model)
                .chatMemoryProvider(id -> MessageWindowChatMemory.withMaxMessages(5))
                .build();
    }

    @Bean
    public QdrantClient qdrantClient() {
        QdrantGrpcClient.Builder grpcClientBuilder = QdrantGrpcClient.newBuilder("127.0.0.1", 6334, false);
        return new QdrantClient(grpcClientBuilder.build());
    }
//    @Bean
//    public EmbeddingStore<TextSegment> embeddingStore() {
//        return QdrantEmbeddingStore.builder()
//                .host("127.0.0.1")
//                .port(6334)
//                .collectionName("cvai")
//                .build();
//    }

    @Bean
    public EmbeddingModel embeddingModel(){
        return OpenAiEmbeddingModel.builder()
                .apiKey(llMproperties.getAlyKey())
                .modelName(llMproperties.getEmbeddingModel())
                .baseUrl(llMproperties.getBaseUrl())
                .build();
    }

    @Bean
    public ChatLanguageModel chatLanguageModel(){
        return OpenAiChatModel.builder()
                .apiKey(llMproperties.getAlyKey())
                .modelName(llMproperties.getCommonModel())
                .baseUrl(llMproperties.getBaseUrl())
                .build();
    }
}